What are Unary Operators?
Unary operators operate on a single operand. Such operators are called unary operators.
Types of Unary Operators
- Unary minus (–)
- Increment (++)
- Decrement (--)
Unary Minus
The minus operator (–) changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive.
Unary minus is different from the subtraction operator, as subtraction requires two operands. If you want to dive deeper into how these operators work in conjunction with data structures.
Example:
#include < sidio.h>
int main() {
int positiveInteger = 100;
int negativeInteger = -positiveInteger;
printf("Positive Integer = %d\n", positiveInteger);
printf("Negative Integer = %d", negativeInteger);
return 0;
}
Output:
Positive Integer = 100
Negative Integer = -100